home *** CD-ROM | disk | FTP | other *** search
- /* getcmt.c - get comments from a C or C++ source file */
- /*
- Byte_Magic Software
- 9850 Meadowglen Ln. #35
- Houston, Tx. 77042
- (713) 975-9033
-
- Author: Greg Messer
- Program: getcmt.c
- Purpose: Isolate comments from a C or C++ source file. Filter.
- Syntax: getcmt (use redirection for file and/or device I/O)
- Release: Released to the Public Domain by Byte_Magic Software.
- Date: 07-11-88 GM...
- First release.
- Revised: 01-13-90 GM...
- Streamlined logic.
- 01-15-90 Bob Stout...
- Fixed unsigned char error as return from getc().
- 01-22-90 GM...
- Fixed bug handling "xx/" (xx = **) at end of comment.
- Added C++ comment extraction.
- Added return of count to OS (ERRORLEVEL in MS-DOS).
- System: Compiled with Microsoft QuickC/Assembler V2.01 under MS-DOS 3.10
- for IBM PCs and compatibles.
- Rules: ANSI C comments begin with /x and end with x/. (x = *).
- Comments do not nest and do not appear in string or character
- constants.
- C++ comments begin with double slashes and end at EOL.
- A Microsoft extension to C allows C++ style comments to serve as
- single-line comments in C source.
- Comments: Useful for creating documentation and improving commenting style.
- Input and output are from stdin and stdout respectively, so use DOS
- redirection for input and output. Messages go to stderr so they
- are not redirectable away from the screen.
- Returns ERRORLEVEL = number of comments in source file(s).
- Examples:
- Example... Output to screen:
- getcmt < cfile.c
- (displays comments from cfile.c on screen)
- type cfile.c | getcmt
- (same as above but slightly slower)
- getcmt < cfile.c | more
- (same as above, but pauses after each full screen)
-
- Example... Output to printer:
- getcmt < cfile.c > prn
- (same as above but prints output on printer)
- type cfile.c | getcmt > prn
- (same as above but slightly slower)
-
- Example... Output to file:
- getcmt < cfile.c > cfile.cmt
- (writes cfile.c comments to cfile.cmt, overwriting existing file)
- getcmt < cfile.c >> cfile.doc
- (writes cfile.c comments to end of cfile.doc (appends))
-
- For complete instructions on using redirection symbols, consult
- the PC-DOS or MS-DOS manual or a general DOS reference book.
- */
-
- #include <stdlib.h>
- #include <stdio.h>
-
- #define LOOKS_GREAT 0
- #define LESS_FILLING 1
-
- int extract_c_cmts(void);
- void inside_c_cmt(int);
-
- /* * * * * * * * * * * * * * * * * * * */
-
- main() /* main logic: */
- {
- register int i;
- char *hype = {
- "\nGETCMT v1.1 - GET CoMmenTs\nby Byte_Magic Software"
- };
- char *help = {
- "\nUsage: GETCMT < sourcefile.ext [> destination file or device]\n"
- "\nReading source code from stdin (Ctrl-C to quit before EOF) ...\n"
- };
- /* display messages to operator */
- #if LOOKS_GREAT
- fputs(hype, stderr);
- fputs(help, stderr);
- #elif LESS_FILLING
- i = 0;
- while(hype[i] != '\0')
- putc(hype[i++], stderr);
- i = 0;
- while(help[i] != '\0')
- putc(help[i++], stderr);
- #endif
-
- i = extract_c_cmts(); /* extract comments in stdin */
- putc('\n', stdout);
-
- return(i); /* return number of comments to */
- /* OS (ERRORLEVEL in DOS) */
- }
-
- /* * * * * * * * * * * * * * * * * * * */
-
- int extract_c_cmts() /* comment extraction logic: */
- {
- register int chi, cht; /* chi = char in, cht = char test */
- int count; /* count = comment count */
-
- count = 0;
- chi = getc(stdin);
- while(chi != EOF) /* as long as there is input... */
- {
- if(chi == '/') /* process comments */
- {
- cht = getc(stdin);
- if(cht == EOF)
- return(count);
- if(cht == '*' || cht == '/') /* if start of a comment... */
- {
- count++; /* count it and */
- inside_c_cmt(cht); /* output all of the comment */
- }
- else
- ungetc(cht, stdin);
- }
- chi = getc(stdin); /* continue scanning input */
- }
- return(count);
- }
-
- /* * * * * * * * * * * * * * * * * * * */
-
- void inside_c_cmt(int ch) /* comment output logic: */
- { /* input ch = either '*' for C */
- /* or '/' for C++ */
-
- register int chi, cht; /* chi = char in, cht = char test */
-
- if(ch == '/') /* make ch = '\n' if C++ */
- ch = '\n'; /* note: ch is already 1st char */
- /* of end comment if this is C */
- putc('\n', stdout);
- chi = getc(stdin);
- while(chi != EOF) /* as long as there is input... */
- { /* process comments */
- if(chi == ch)
- {
- if(ch == '\n') /* if C++ comment is ended... */
- return; /* stop outputting */
- cht = getc(stdin);
- if(cht == '/') /* if C comment is ended... */
- return; /* stop outputting */
- else
- {
- ungetc(cht, stdin);
- putc(chi, stdout);
- }
- }
- else
- putc(chi, stdout); /* else comment text, output it */
-
- chi = getc(stdin); /* continue scanning input */
- }
- return;
- }
-
- /* * * * * * * * * * * * * * * * * * * */
- /* end of getcmt.c */
-
-